
package com.android.formula.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.widget.GridView;

import com.android.formula.R;
import com.android.formula.adapter.FormulaListAdapter;
import com.android.formula.adapter.TBaseAdapter;
import com.android.formula.model.Formula;
import com.android.formula.model.TypeSubSubject;
import com.android.formula.task.GetFormulaDBTask;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.utils.CommonUtils;
import com.android.formula.utils.Constants;

public class FormulaActivity extends BaseGridViewActivity<Formula> {

    @Override
    public void initView() {
        super.initView();
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_formula;
    }

    @Override
    protected GridView getGridView() {
        return (GridView) findViewById(R.id.formula_gridview);
    }

    @Override
    protected void clickOnPos(Formula clickedItem, int pos) {

    }

    @Override
    protected TBaseAdapter<Formula> getAdapter() {
        return new FormulaListAdapter(this, mListItemDatas);
    }

    @Override
    protected void getData() {
        super.getData();
        Bundle bun = getIntent().getExtras();
        TypeSubSubject param = (TypeSubSubject) bun.getSerializable(Constants.DATA_KEY);
        this.setTitle(param.getSsSubjectName());
        getDialogsManager().showProgress(R.string.loading_msg);

        GetFormulaDBTask getFormulaDBTask = new GetFormulaDBTask(this, param,
                new IDataEventHandler<ArrayList<Formula>>() {

                    @Override
                    public void onNotifyNoData() {
                        getDialogsManager().dismissProgress();
                        showNoData();
                    }

                    @Override
                    public void onNotifyErrorMessage(String title, String message) {
                        getDialogsManager().dismissProgress();
                        showNoData();
                        getDialogsManager().showErrorDialog(R.string.error_title,
                                R.string.error_msg, 0, null);
                    }

                    @Override
                    public void onNotifyData(ArrayList<Formula> data) {
                        getDialogsManager().dismissProgress();
                        mListItemDatas.addAll(data);
                        if (mListItemDatas != null && mListItemDatas.size() > 0) {
                            showData();
                        } else {
                            showNoData();
                        }
                    }
                });
        CommonUtils.executeAsyTask(getFormulaDBTask, param);
    }

}
